<?php

namespace core;

use \core\ServiceInstanceFactory;

/*
 * Service Invoke
 *
 * @author Kevin - xuhuahai@dangdang.com
 * @version 1.0
 * @date Apr 18, 2015
 */
class ServiceInvoke {
	
	/**
	 * 调用远程SOA服务
	 * 调用这个方法的前提是：需要部署soa_proxy
	 * @param unknown $param_group
	 * @param unknown $param_service
	 * @param unknown $param_version
	 * @param unknown $trace_id      
	 * @param unknown $trace_span_id
	 * @param unknown $request_params
	 * @return unknown
	 */
	public static function invokeRemoteSoaService($param_group, $param_service, $param_version, 
			$trace_id, $trace_span_id, $request_params) {
		$time_start = get_now_microtime_float();
		$request = array (
				'group'   => $param_group,
				'service' => $param_service,
				'version' => $param_version,
				'trace_id'=> $trace_id,
				'trace_span_id' => $trace_span_id,
				'data' => $request_params 
		);
		$req_msg = json_encode ( $request );
		$response = invoke_service ( $param_group, $param_service, $param_version, $req_msg );
		if ($response != null) {
			$response = json_decode ( $response, TRUE );
		}
		$time_cost = (get_now_microtime_float() - $time_start) * 1000;
		soa_info('DAPPER::: ' . $trace_id . ',' . $trace_span_id . ',' . get_remote_service_coordinate($param_group, $param_service, $param_version). ' ; cost time (ms) = '. $time_cost . ' ');
		return $response;
	}
	
	/**
	 * 调用本地SOA服务
	 * @param unknown $param_group
	 * @param unknown $param_service
	 * @param unknown $param_version
	 * @param unknown $trace_id
	 * @param unknown $trace_span_id
	 * @param unknown $request_params
	 * @return NULL|unknown
	 */
	public static function invokeLocalSoaService($param_group, $param_service, $param_version, 
			$trace_id, $trace_span_id, $request_params) {
		$time_start = get_now_microtime_float();
		$instance = ServiceInstanceFactory::getInstance($param_group, $param_service, $param_version);
		if (is_null($instance)) {
			return null;
		}
		$current_trace_span_id = get_service_coordinate($param_group, $param_service, $param_version);
		$resp = $instance->execute($trace_id, $current_trace_span_id, $request_params);
		$time_cost = (get_now_microtime_float() - $time_start) * 1000;
		soa_info('DAPPER::: ' . $trace_id . ',' . $trace_span_id . ',' . $current_trace_span_id. ' ; cost time (ms) = '. $time_cost . ' ');
		return $resp;
	}
	
	/**
	 * 调用服务，不区分本地或者远程
	 * 这个方法效率不高，因为它会先尝试从本地访问，然后再尝试从远程访问，不建议使用
	 * 建议使用确切的方法，比如invokeLocalSoaService和invokeRemoteSoaService
	 * @param unknown $param_group
	 * @param unknown $param_service
	 * @param unknown $param_version
	 * @param unknown $trace_id
	 * @param unknown $trace_span_id
	 * @param unknown $request_params
	 * @return Ambigous <\core\util\unknown, NULL>
	 */
	public static function invokeSoaService($param_group, $param_service, $param_version, 
			$trace_id, $trace_span_id, $request_params) {
		$instance = ServiceInstanceFactory::getInstance($param_group, $param_service, $param_version);
		if (is_null($instance)) {
			$resp = ServiceInvoke::invokeRemoteSoaService($param_group,$param_service,$param_version,
					$trace_id, $trace_span_id, $request_params);
		} else {
			$resp = ServiceInvoke::invokeLocalSoaService($param_group,$param_service,$param_version,
					$trace_id, $trace_span_id, $request_params);
		}
		return $resp;
	}
	
	
}


